package com.example.basic_ui_demo.view_model

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.example.basic_ui_demo.data_class.news.News

class NewsViewModel : ViewModel() {
    private val _newsList = mutableStateListOf<News>()

    //新闻列表
    val newsList get() = _newsList
    //外露的状态

    private val _bannerList = mutableStateListOf<News>()

    //banner列表
    val bannerList get() = _bannerList
    //外露的状态

    private val _defaultText = mutableStateOf("请输入搜索内容")
    //搜索框的默认文本

    val defaultText get() = _defaultText

    private val _searchText = mutableStateOf("")

    //实际的搜索文本
    val searchText get() = _searchText
    //外露的搜索文本状态,如果失去焦点且之前输入的搜索值为空，则显示defaultText，否则显示searchText

    val isLoading = mutableStateOf(true)

    val isError = mutableStateOf(false)

    fun onTextChanged(newText: String) {
        _searchText.value = newText
    }
    // 输入文本

    fun addNews(news: News) {
        _newsList.add(news)
    }

    fun addNewsFront(news: News) {
        _newsList.add(0, news)
    }

    fun replaceNewsList(inputNewsList: List<News>?) {
        inputNewsList?.let {
            _newsList.clear()
            _newsList.addAll(it)
        }
    }

    fun changeBanner(list: List<News>) {
        _bannerList.clear()
        _bannerList.addAll(list)
    }
}